import { observable } from "mobx";
import apiAxios from '@/utils/requestAxios';
import api from "../../utils/api"
import { check } from '@/utils/check';
import { sto } from "@/utils/static2";
import indexStore from "../model"
// 指纹操作
import FingerApi from "../finger_device/model";

const store = observable({
    // 加载
    loading: false,
    // 患者listy
    patientList: [],
    // 患者list总数
    total: "",
    // 指纹信息弹框
    isModalOpen: false,
     // 详细信息弹框
    isModalOpen2: false,
    // 指纹信息
    fingerPrintList: [],
    // 患者id（patientId）
    patientId: "",
    // 要删除的指纹id
    delId: "",
    // form数据
    form: {
        name: "",
        department: "",
        hospitalizationId: "",
        outpatientId: "",
    },
    // 分页
    pageNum: 1,
    // 判断是搜索还是分页
    flag: "",
    // 存储扫描到的指纹字符串
    fingerStr: "",
    // 扫描动画弹框
    isScan: false,
    // 有没有识别到指纹
    isFinger: false,
    // scan定时器
    timer: "",


    //获取患者列表数据
    async getPatientList() {
        store.loading = true
        let parms = {}
        if(store.flag === "搜索") {
            parms = {
                pageNum: 1,
                pageSize: 6666,
                ...store.form
            }
        }else{
            parms = {
                pageNum: store.pageNum,
                pageSize: 6666,
                ...store.form
            }
        }
        const res = await apiAxios('POST', api.personPatientList(), parms);
        store.loading = false
        if (!check(res)) return;
        if (res && res.data && res.data.rows) {
            store.total = res.data.total
           // store.patientList = res.data.rows.reverse() //.filter(e=>e.deleteFlag==false)
            // 获取指纹用户指纹列表做‘指纹数量’对应插值
            store.getFingerPrintList(res.data.rows.reverse())
        }
    },
    // 获取指纹数据列表
    async getFingerPrintList(tList) {
        let res = await apiAxios("POST", api.fingerPrintList(), {})
        if (!check(res)) return;
        if (res?.data?.rows) {
            const list = res.data.rows;
            // 获取指纹用户指纹列表做‘指纹数量’对应插值
            tList.map((tItem) => {
                const listIndex = list.findIndex((lItem) => lItem.patientId === tItem.id);
                if (listIndex !== -1) {
                    tItem.fingerprintNum = list[listIndex].fingerprintNum;
                }
                return tItem;
            });
            store.patientList = [...tList];
        }
    },
    // 患者详细指纹信息
    async getFingerPrintDetail() {
        let res = await apiAxios("GET", `${api.fingerPrintDetail()}/${store.patientId}`,)
        if (!(check(res))) return
        if (res) {
            store.fingerPrintList = res.data
            // 查到就打开弹框
            store.isModalOpen = true
        }
    },
    // 指纹录入/添加
    async addFingerPrint() {
        let parms = {
            // "finger": "中指",
            "fingerprintStr": store.fingerStr,
            "patientId": store.patientId
        }
        let res = await apiAxios("POST", api.addFingerPrint(), parms)
        if (!(check(res))) return
        if (res) {
            store.getFingerPrintDetail()
        }
    },
    // 删除指纹
    async delFingerPrint() {
        let res = await apiAxios('DELETE', `${api.delFingerPrint()}/${store.delId}`)
        if (!(check(res))) return
        if (res) {
            // 删除成功更新列表
            store.getFingerPrintDetail()
        }
    },
    // 点击删除
    del(e) {
        store.delId = e.id
        store.delFingerPrint()
    },
    // 扫描
    scan() {

        store.isFinger = false
        // 打开动画
        store.isScan = true
        let timer = setInterval(() => {
            FingerApi.WEB_FINGER_SCAN()
            // 扫描到指纹后，关闭定时器/添加指纹/关闭动画
            if(store.isFinger){
                store.isScan = false
                if(timer){
                    clearInterval(timer)
                }
                store.addFingerPrint()
            }
        }, 200)
        store.timer = timer
    },

})

export default store